-- danger_fire
-- create by panyl
-- 攻击受创（技能规则）

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力
        local force = para[1];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        local combine = PropM.combine;
        local PropM_apply = PropM.apply;
        local fetchProps = PropM.fetchProps;
        local trigger = PropM.trigger;

        --最终威力
        local force = math.modf(force * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        local prop = combine(target, "weak_enemy", "magic");
        magic = PropM_apply(prop, magic);

        -- 被对方绝对值削弱
        prop = combine(target, "weak_enemy2", "magic");
        magic = PropM_apply(prop, magic);

        -- 3. 伤害/计算伤害时魔力+3
        local damage = math.modf(force * (magic + 3) / 1000);

        -- 魔法暴击类，多种暴击线性叠加伤害
        local addon_damage = 0;
        local rand = DungeonM.getRandSeed("danger_fire") % 100;
        -- 随机2~8倍伤害
        addon_damage = addon_damage + damage * (2 + rand % 7);

        -- 伤害类魔法概率增强
        local props = fetchProps(source, "damage_magic");
        for _, prop in ipairs(props) do
            addon_damage = addon_damage + trigger(source, prop[1], prop[2], damage);
        end
        -- 魔法暴击
        props = fetchProps(source, "critical_magic");
        for _, prop in ipairs(props) do
            addon_damage = addon_damage + trigger(source, prop[1], prop[2], damage);
        end
        -- 概率双倍
        props = fetchProps(source, "skill_double");
        for _, prop in ipairs(props) do
            if tonumber(prop[2]) == skillId then
                addon_damage = addon_damage + trigger(source, prop[1], prop[2], damage);
            end
        end
        -- 最终伤害
        damage = damage + addon_damage;

        -- 4. 魔法抗性
        prop = combine(target, "resist", "magic");
        -- 忽视抗性
        local p = combine(source, "ignore_resist", skillId);
        prop[3] = PropM_apply(p, prop[3]);
        p = combine(source, "ignore_resist", "magic");
        prop[3] = PropM_apply(p, prop[3]);

        damage = PropM_apply(prop, damage);

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 伤害
        CombatM.receiveDamage(source, target, damage, skillId);

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 威力
        local base = para[1];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 伤害
        local damageBase = math.modf(base * (magic + 3) / 1000);
        local damageForce = math.modf(force * (magic + 3) / 1000);
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{dange_fire}", damageForce);
        else
            desc = string.gsub(desc, "{dange_fire}", damageBase .. "(+" .. damageAddon .. ")");
        end

        desc = string.gsub(desc, "{2}", para[2]);
        desc = string.gsub(desc, "{4}", para[3]);
        desc = string.gsub(desc, "{8}", para[4]);

        -- 系别TODO

        return desc;
    end,
};
